package com.example.himalaya.presenters;

import android.widget.Toast;

import com.example.himalaya.base.BaseApplication;
import com.example.himalaya.view.callback.ILoveSongDaoCallback;
import com.example.himalaya.view.callback.ILoveSongViewCallback;
import com.example.himalaya.constants.Constants;
import com.example.himalaya.model.dao.LoveSongDao;
import com.example.himalaya.presenters.interfaces.ILoveSongPresenter;
import com.ximalaya.ting.android.opensdk.model.track.Track;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.schedulers.Schedulers;


public class LoveSongPresenter implements ILoveSongPresenter, ILoveSongDaoCallback {

    private static LoveSongPresenter instance;

    public static LoveSongPresenter getInstance() {
        if (instance == null) {
            synchronized (LoveSongPresenter.class) {
                if (instance == null) {
                    instance = new LoveSongPresenter();
                }
            }
        }
        return instance;
    }


    // Constant
//    private static final String TAG = "LoveSongPresenter";

    // Global variable

    //List
    private List<ILoveSongViewCallback> mCallbacks = new ArrayList<>();
    private List<Track> mData;

    // Logic object
    private final LoveSongDao mLoveSongDao;


    private LoveSongPresenter() {
        // init data
        mData = new ArrayList<>();

        // create logic object
        mLoveSongDao = new LoveSongDao();

        // register callback
        mLoveSongDao.registerCallback(this);

        // init data (get data from db)
        listLoveSongs();
    }


    /**
     * 该歌曲是否已经点了我喜欢
     */
    public boolean isLoved(Track track) {
        if (mData != null) {
            for (Track datum : mData) {
                if (datum.getDataId() == track.getDataId()) {
                    return true;
                }
            }
        }

        return false;
    }


    @Override
    public void addLoveSong(Track track) {
        // 判断是否满足用户限制
        if (mData != null) {
            if (mData.size() > Constants.MAX_LOVE_SONG_COUNT) {
                Toast.makeText(BaseApplication.getAppContext(), "听的歌太多，需要删除一首老歌了", Toast.LENGTH_LONG).show();


                delLoveSong(mData.get(mData.size() - 1));
            } else {
                doAddLoveSong(track);
            }
        }
    }

    private void doAddLoveSong(final Track track) {


        Observable.create(new ObservableOnSubscribe<Object>() {

            @Override
            public void subscribe(ObservableEmitter<Object> emitter) {
                mLoveSongDao.addLoveSong(track);
            }
        }).subscribeOn(Schedulers.io()).subscribe();
    }

    @Override
    public void clearLoveSong() {
        Observable.create(new ObservableOnSubscribe<Object>() {

            @Override
            public void subscribe(ObservableEmitter<Object> emitter) {
                mLoveSongDao.clearLoveSong();
            }
        }).subscribeOn(Schedulers.io()).subscribe();
    }

    @Override
    public void delLoveSong(final Track track) {
        Observable.create(new ObservableOnSubscribe<Object>() {

            @Override
            public void subscribe(ObservableEmitter<Object> emitter) {
                mLoveSongDao.delLoveSong(track);
            }
        }).subscribeOn(Schedulers.io()).subscribe();
    }

    @Override
    public void listLoveSongs() {
        Observable.create(new ObservableOnSubscribe<Object>() {

            @Override
            public void subscribe(ObservableEmitter<Object> emitter) {
                mLoveSongDao.listLoveSongs();
            }
        }).subscribeOn(Schedulers.io()).subscribe();
    }

    @Override
    public void registerViewCallback(ILoveSongViewCallback iLoveSongViewCallback) {
        if (mCallbacks != null) {
            if (!mCallbacks.contains(iLoveSongViewCallback)) {
                mCallbacks.add(iLoveSongViewCallback);
            }
        }
    }

    @Override
    public void unRegisterViewCallback(ILoveSongViewCallback iLoveSongViewCallback) {
        mCallbacks.remove(iLoveSongViewCallback);
    }

    @Override
    public void onLoveSongAdd(boolean isSuccess) {

        listLoveSongs();

        for (ILoveSongViewCallback callback : mCallbacks) {
            callback.onLoveSongAdd(isSuccess);
        }
    }

    @Override
    public void onLoveSongDel(boolean isSuccess) {
        listLoveSongs();

        for (ILoveSongViewCallback callback : mCallbacks) {
            callback.onLoveSongDel(isSuccess);
        }
    }

    @Override
    public void onLoveSongLoaded(final List<Track> tracks) {
        if (mData != null) {
            mData.clear();
            mData.addAll(tracks);

            BaseApplication.getsHandler().post(new Runnable() {
                @Override
                public void run() {
                    for (ILoveSongViewCallback callback : mCallbacks) {
                        callback.onLoveSongLoaded(tracks);
                    }
                }
            });
        }
    }

    @Override
    public void onLoveSongClean(boolean isSuccess) {
        listLoveSongs();

        for (ILoveSongViewCallback callback : mCallbacks) {
            callback.onLoveSongClean(isSuccess);
        }
    }
}
